package org.dromara.trace.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.trace.domain.vo.TraceSellproVo;
import org.dromara.trace.domain.bo.TraceSellproBo;
import org.dromara.trace.service.ITraceSellproService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 溯源产品
 *
 * @author Robin
 * @date 2024-12-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/trace/sellpro")
public class TraceSellproController extends BaseController {

    private final ITraceSellproService traceSellproService;

    /**
     * 查询溯源产品列表
     */
    @SaCheckPermission("trace:sellpro:list")
    @GetMapping("/list")
    public TableDataInfo<TraceSellproVo> list(TraceSellproBo bo, PageQuery pageQuery) {
        return traceSellproService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出溯源产品列表
     */
    @SaCheckPermission("trace:sellpro:export")
    @Log(title = "溯源产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TraceSellproBo bo, HttpServletResponse response) {
        List<TraceSellproVo> list = traceSellproService.queryList(bo);
        ExcelUtil.exportExcel(list, "溯源产品", TraceSellproVo.class, response);
    }

    /**
     * 获取溯源产品详细信息
     *
     * @param sellproId 主键
     */
    @SaCheckPermission("trace:sellpro:query")
    @GetMapping("/{sellproId}")
    public R<TraceSellproVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long sellproId) {
        return R.ok(traceSellproService.queryById(sellproId));
    }

    /**
     * 新增溯源产品
     */
    @SaCheckPermission("trace:sellpro:add")
    @Log(title = "溯源产品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TraceSellproBo bo) {
        return toAjax(traceSellproService.insertByBo(bo));
    }

    /**
     * 修改溯源产品
     */
    @SaCheckPermission("trace:sellpro:edit")
    @Log(title = "溯源产品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TraceSellproBo bo) {
        return toAjax(traceSellproService.updateByBo(bo));
    }

    /**
     * 删除溯源产品
     *
     * @param sellproIds 主键串
     */
    @SaCheckPermission("trace:sellpro:remove")
    @Log(title = "溯源产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sellproIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] sellproIds) {
        return toAjax(traceSellproService.deleteWithValidByIds(List.of(sellproIds), true));
    }
}
